<?php
/**
 * Manages the javascript client code.
 */
class fily_ClientCodeManager {
    /**
     * Array with the javascript code.
     */
    protected static $code = array();

    /**
     * adds a client code
     * @param string $code javascript code
     * @param int $executionZone the zone where the ajax panel javascript code will be excuted
     */
    public static function addCode($code) {
        self::$code[] = $code;
    }


    /**
     * returns all javascript code part sorted by exectution zone
     * @return string
     */
    public static function getClientCode() {
        $allCode = " \n //generated by firelynx \n";
        foreach(self::$code as $code) {
            $allCode .= $code."\n";
        }
        $allCode .= "\n";
        return $allCode;
    }

    /**
     * returns all javascript code part sorted by exectution zone
     * and automatically calls this code on the onload event from html body
     * @return string
     */
    public static function getPageClientCode() {
        return "function firelynx_oninit() {\n". self::getClientCode() ."var eventArgs = firelynx.EventArgs(null,firelynx, 'pageLoad');firelynx.fireEvent(eventArgs);\n}\nfirelynx.addEvent(window,'load', firelynx_oninit);" ;
    }

    private static $usedIds = array();

    /**
     * checks if the id is used in a javascript code
     * @param string $id
     * @return boolean
     */
    public static function idIsUsed($id){
        $inUse = false;
        foreach ( self::$usedIds as $value) {
            if($id==$value){
                $inUse = true;
                break;
            }
        }
        return $inUse;
    }


    /**
     *  call this function if you use an id in javascript code
     * @param string $id
     */
    public static function useId($id){
        self::$usedIds[] = $id;
    }


}
?>
